﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using MITI;
using MapControl.Misc;
using MapControl.Misc.DAL;

namespace GPSMap.Core.Misc.DAL
{
    public class Plane
    {
        public int Id;
        public string Code;
        public string Name;

        public Plane()
        {

        }

        public Plane(int id, string code, string name)
        {
            try
            {
                Id = id;
                Code = code;
                Name = name;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Plane(DataRow row)
        {
            try
            {
                Id = (int)row["id"];
                Code = row["code"].ToString();
                Name = row["name"].ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override string ToString()
        {
            // Generates the text shown in the combo box
            return Name;
        }
    }

    public class PlaneRepository
    {
        private BaseDBCe2 _dbConnect;

        public PlaneRepository()
        {
            _dbConnect = new BaseDBCe2(GeneralClass.appDBFile);
        }

        ///// <summary>
        ///// Xóa dữ liệu mẫu trong table Plane
        ///// </summary>
        //public void ResetData()
        //{
        //    //===============================================================
        //    //***************************************************************
        //    //Xóa dữ liệu cũ của Plane có tên là 'heli'
        //    string code = "heli";
        //    string sql = string.Format("select * from plane where code = N'{0}'", code);
        //    if (_dbConnect.ExecSql_DataSet(sql).Tables[0].Rows.Count == 1)
        //        Delete(code);
        //    //***************************************************************
        //    //===============================================================

        //}

        public bool IsExist_Code(string code)
        {
            string sql = string.Format("select * from plane where code = N'{0}'", code);
            if (_dbConnect.ExecSql_DataSet(sql).Tables[0].Rows.Count > 0)
                return true;
            return false;
        }

        public bool IsExist_Name(string code)
        {
            string sql = string.Format("select * from plane where name = N'{0}'", code);
            if (_dbConnect.ExecSql_DataSet(sql).Tables[0].Rows.Count > 0)
                return true;
            return false;
        }

        public DataTable GetAll()
        {
            string sql = "select * from plane";
            return _dbConnect.ExecSql_DataSet(sql).Tables[0];
        }

        public Plane GetPlane(string code)
        {
            string sql = string.Format("select * from plane where code = '{0}'", code);
            DataTable db = _dbConnect.ExecSql_DataSet(sql).Tables[0];
            Plane objPlane = null;
            if (db.Rows.Count > 0)
                objPlane = new Plane(db.Rows[0]);
            return objPlane;
        }

        public string GetPlaneName(string code)
        {
            string sql = string.Format("select name from plane where code = '{0}'", code);
            string name = _dbConnect.ExecSql_DataValue(sql).ToString();
            return name;
        }
        //public Plane GetPlane(int id)
        //{
        //    string sql = string.Format("select * from plane where id = {0}", id);
        //    DataTable db = _dbConnect.ExecSql_DataSet(sql).Tables[0];
        //    Plane objPlane = null;
        //    if (db.Rows.Count > 0)
        //        objPlane = new Plane(db.Rows[0]);
        //    return objPlane;
        //} 

        public void Insert(Plane objPlane)
        {
            string sql = "";
            try
            {
                sql = string.Format("insert into plane(code, name) values(N'{0}',N'{1}')", objPlane.Code, objPlane.Name);
                _dbConnect.ExecSql_NoneQuery(sql);
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

        public void Update(Plane objPlane)
        {
            string sql = "";
            try
            {
                sql = string.Format("update plane set code = N'{0}', name = N'{1}' where id= {2}",
                                    objPlane.Code, objPlane.Name, objPlane.Id);
                _dbConnect.ExecSql_NoneQuery(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Delete(int id)
        {
            string sql = "";
            try
            {
                sql = string.Format("delete from plane where id = '{0}'", id);
                _dbConnect.ExecSql_NoneQuery(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void DeleteAll()
        {
            string sql = "";
            try
            {
                sql = "delete from plane";
                _dbConnect.ExecSql_NoneQuery(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

   